<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Company extends MY_Luckydog {
    public function __construct()
    {

        parent::__construct();
        $this->load->model('luckydog/Company_model', 'company');
    }
	public function index()
	{
        $_SESSION['v_menu'] = 'company';
        $result = $this->company->get_company_list();
        foreach ($result as $key => $value) {
            $area_list = $this->company->get_area_list($value['companyid']);
            $result[$key]['area_list'] = $area_list;
        }
        $data=array(
            'company_list'=>$result,
        );

		$this->load->view('luckydog/company/company_list',$data);
	}
    //添加分类 html
	public function add()
	{
        
		$this->load->view('luckydog/company/company_add');
	}
    //修改分类 html
	public function edit()
	{
        $companyid  = $this->input->get("companyid");
        $data = array();
	    if ($companyid){
            $area_list = $this->company->get_area_list($companyid);
            $company_row = $this->company->get_company_by_id($companyid);
            $data=array(
                'company_row'=>$company_row,
                'area_list'=>$area_list
            );
	    }

        
		$this->load->view('luckydog/company/company_edit',$data);
	}
    public function do_add(){
        $name   = $this->input->post("name");
        $contactmobile = $this->input->post("contactmobile");

        $data = array(
            "name"=>$name,
            "contactmobile"=>$contactmobile,
            "addtime"=>time()
        );
        $insert_id = $this->company->add_company($data);

        if($insert_id){
            echo json_encode(array('status'=>1,'message'=>'添加成功'));
        }else{
            echo json_encode(array('status'=>-1,'message'=>'添加失败'));
        }
        exit;
    }
    public function do_edit(){
        $companyid   = $this->input->post("companyid");
        $contactmobile   = $this->input->post("contactmobile");
        $name = $this->input->post("name");
        $summary = $this->input->post('summery',true);
        $data = array(
            "contactmobile"=>$contactmobile,
            "name"=>$name,
            "summary"=>$summary
        );
        if(empty($companyid)){
            $data['addtime'] = time(); 
            $edit_flag = $this->company->add_company($data);
            $companyid = $edit_flag;
        }else{
            $edit_flag = $this->company->edit_company($companyid,$data);
        }
        

        
        $address_arr = $this->input->post('address_arr',true);
        $address_id_arr = $this->input->post('address_id_arr',true);
        $header_arr = $this->input->post('header_arr',true);
        $header_contact_arr = $this->input->post('header_contact_arr',true);
        foreach($address_arr as $k=>$row){
           if(empty($address_id_arr[$k])){
            //添加
                $data = array(
                    "area"=>$address_arr[$k],
                    "header"=>$header_arr[$k],
                    "header_contact"=>$header_contact_arr[$k],
                    "companyid"=>$companyid
                );
                $res = $this->company->add_contact($data);
           }else{
                //更新
                $data = array(
                    "area"=>$address_arr[$k],
                    "header"=>$header_arr[$k],
                    "header_contact"=>$header_contact_arr[$k]
                );
                
                $res = $this->company->update_contact($address_id_arr[$k],$data);
           }
        }


        if($edit_flag){
            echo json_encode(array('status'=>1,'message'=>'修改成功'));
        }else{
            echo json_encode(array('status'=>-1,'message'=>'修改失败'));
        }
        exit;
    }
    public function do_del(){
        $companyid  = $this->input->post("companyid");
        $res = $this->company->del_company($companyid);
        echo json_encode(array('status'=>1));
        exit;
    }

     public function del_address(){
        $address_id  = $this->input->post("address_id");
        $res = $this->company->del_address($address_id);
        echo $res;
        exit;
    }

   

}

/* End of file category.php */
/* Location: ./application/controllers/luckydog/category.php */
